﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Utility;
using Data;

namespace Website.UC
{
    public partial class UCExamination : System.Web.UI.UserControl
    {
        Tbl_ExaminationBus exam_Bus = new Tbl_ExaminationBus();
        Tbl_SubjectBUS subject_Bus = new Tbl_SubjectBUS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
                loadSubject();
            }
        }
        private void loadData()
        {
            GridView1.DataSource = exam_Bus.getListExamination();
            GridView1.DataBind();
        }
        private void loadSubject()
        {
            foreach (tbl_subject m in subject_Bus.getListSubject())
            {
                DropDownList1.Items.Add(m.subject_name + "-" + m.subject_id);
            }
            DropDownList1.DataBind();
        }
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            lblId.Text = WebHelper.GetValueGridView(GridView1, 2, e);
            txtNote.Text = WebHelper.GetValueGridView(GridView1, 5, e);
        }
        private void Clear()
        {
            lblId.Text = "";
            txtNote.Text = "";

        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            tbl_examination te = new tbl_examination();
            te.exam_id = Int32.Parse(WebHelper.GetDataKey(GridView1, e));
            exam_Bus.deleteExamination(te);
            loadData();
            Clear();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtNote.Text.Length == 0)
            {
                WebHelper.ShowMessage("Name not null", Page);
            }
            else
            {
                tbl_examination te = new tbl_examination();
                te.subject_id = Int32.Parse(DropDownList1.SelectedValue.ToString().Split('-')[1].ToString());
                te.exam_note = txtNote.Text;
                te.exam_date = Calendar1.SelectedDate.Date;
                if (lblId.Text.Length == 0)
                {

                    exam_Bus.insertExamination(te);
                }
                else
                {
                    te.exam_id = Int32.Parse(lblId.Text);
                    exam_Bus.updateExamination(te);
                }
                Clear();
                loadData();
            }

        }
    }
}